<template>
  <div class="content">
    <div class="headBtn">
      <jc-button
        :btn-arr="['reload', 'option']"
        :disab-arr="['close', 'cancel', 'com1', 'com2', 'using', 'freeze']"
        @refresh="handleGetUntreated"
        @toExcel="handleExport"
      />
    </div>
    <div class="header">
      <div class="header-name">
        <search
          :options="selectData"
          :search-son="searchSon"
          :msg="tacitly"
          @seek="collect"
          @hand="handleQuerySonClass"
        />
        <el-button type="primary" class="btn" size="mini" @click="handleQuerySonClass">{{ $t('btn.search') }}</el-button>
        <filtration
          :ftype="ftype"
          :msg="tacitly"
          :select-data="selectData"
          :table-header="tableHeader1"
          :detail-header="tableHeader"
          @seachData="handleQuerySonClass"
        />
      </div>
    </div>
    <div class="table-content">
      <jc-table
        v-loading="loading"
        :table-data="tableData"
        :table-header="tableHeader"
        :table-height="tableHeight"
        serial
        sortable
        :get-summaries="getSummaries"
        show-summary
      />
      <div class="footer">
        <jc-pagination
          :total="total"
          :page.sync="currentPage"
          :limit.sync="size"
          @pagination="handleGetUntreated"
        />
      </div>
    </div>
  </div>
</template>

<script>
import {
  queryJcCrPmCywj
} from '@/api/production/productionList'
import search from '@/components/Search'
import searData from '@/components/Search/mixin'
import { toMxAmina, Forbiders } from '@/components/ToMxamineState'
import { export_json_to_excel, formatJson } from '@/utils/Export2Excel'
import filtration from '@/components/filtration'
import jcButton from '@/components/Button'

export default {
  components: {
    jcButton,
    search,
    filtration
  },
  mixins: [searData],
  data() {
    return {
      ftype: 58,
      // 表头
      tableData: [], // 销售数据
      tableHeight: '70vh',
      tableHeader: [
        { label: this.$t('market.fdate'), prop: '日期', width: '100' },
        { label: this.$t('market.order'), prop: '销售订单编号', width: '100' },
        { label: this.$t('market.cust'), prop: '客户', width: '100px' },
        { label: this.$t('market.productNo'), prop: '产品编码', width: '120' },
        { label: this.$t('market.productName'), prop: '产品名称', width: '100' },
        { label: this.$t('market.productDesc'), prop: '产品描述', width: '200' },
        { label: this.$t('market.fmodel'), prop: '型号' },
        { label: this.$t('market.proSpecifica'), prop: '产品尺寸规格' },
        { label: this.$t('market.workshop'), prop: '生产车间', width: '100' },
        { label: this.$t('market.strength'), prop: '编制', width: '100' },
        { label: this.$t('market.fqty'), prop: '数量', width: '60px' },
        { label: this.$t('market.funit'), prop: '单位', width: '60px' },
        { label: this.$t('market.note'), prop: '备注', width: '130' },
        { label: this.$t('market.planfinishDate'), prop: '计划完工时间', width: '100' },
        { label: this.$t('market.orderTime'), prop: '下单时间', width: '100' },
        { label: this.$t('market.packTime'), prop: '包装完工时间', width: '100' },
        { label: this.$t('market.placeTime'), prop: '座板/弹簧/PE袋(物料到位时间)', width: '100px' },
        { label: this.$t('market.forthTime'), prop: '泡沫(物料到位时间)', width: '100' },
        { label: this.$t('market.cartonTime'), prop: '纸箱(物料到位时间)', width: '100' },
        { label: this.$t('market.partsTime'), prop: '组装配件(物料到位时间)', width: '100' }
      ],
      total: 0,
      size: 200,
      currentPage: 1,
      tableHeader1: [],
      val: []
    }
  },
  created() {
    // this.handleGetUntreated()
    this.tableHeader1 = this.tableHeader
    this.tableHeight = window.innerWidth > 1440 ? 'calc(100vh - 230px)' : '73vh'
  },
  methods: {
    // 导出表格
    handleExport() {
      require.ensure([], () => {
        const tHeader = this.tableHeader.map((res) => {
          return res.label
        })
        const filterVal = this.tableHeader.map((res) => {
          return res.prop
        })
        if (this.tableData.length <= 0) {
          this.$message.warning('没有导出数据')
        } else if (this.val && this.val.length > 0) {
          const list = this.val.map(item => {
            return (toMxAmina(item), Forbiders(item))
          })
          const data = formatJson(filterVal, list)
          export_json_to_excel(tHeader, data, '五金报表')
        } else {
          this.sd = ''
          this.ed = ''
          this.salKhddh = ''
          this.searCollData.forEach(res => {
            if (res.field === 'sd') {
              this.sd = res.seach
            } else if (res.field === 'ed') {
              this.ed = res.seach
            } else if (res.field === 'salKhddh') {
              this.salKhddh = res.seach
            }
          })
          queryJcCrPmCywj({
            pageNum: 1,
            pageSize: 100000,
            salKhddh: this.salKhddh,
            sd: this.sd,
            ed: this.ed,
            search: this.searCollData
          }).then(res => {
            if (res.code === 0) {
              const list = res.data.list.map(item => {
                return (toMxAmina(item), Forbiders(item))
              })
              const data = formatJson(filterVal, list)
              export_json_to_excel(tHeader, data, '五金报表')
            } else {
              this.$confirm(res.message, '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'error'
              })
            }
          })
        }
      })
    },
    // 获取五金报表数据
    async handleGetUntreated(ev) {
      this.loading = true
      this.details = []
      if (ev && !ev.page) {
        this.details = ev
      }
      this.sd = ''
      this.ed = ''
      this.salKhddh = ''
      this.searCollData.concat(this.details).forEach(res => {
        if (res.field === 'sd') {
          this.sd = res.seach
        } else if (res.field === 'ed') {
          this.ed = res.seach
        } else if (res.field === 'salKhddh') {
          this.salKhddh = res.seach
        }
      })
      const DATA = {
        pageNum: this.currentPage,
        pageSize: this.size,
        salKhddh: this.salKhddh,
        sd: this.sd,
        ed: this.ed,
        search: this.searCollData.concat(this.details)
      }
      const RES = await queryJcCrPmCywj(DATA)
      if (RES.code === 0) {
        this.loading = false
        this.tableData = RES.data.list
        this.total = RES.data.total
      } else {
        this.loading = false
        this.$confirm(RES.message, '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'error'
        })
      }
    },
    // 搜索
    handleQuerySonClass(ev) {
      this.currentPage = 1
      if (ev && ev.fname) {
        this.searchSon = ev.detail
        this.customHandle(ev)
        this.handleGetUntreated(ev.detail)
      } else {
        this.handleGetUntreated()
      }
    },
    // 自定义列表
    customHandle(ev) {
      this.tableHeaders = []
      if (ev.header) {
        ev.header.forEach(item => {
          this.tableHeader1.forEach(res => {
            if (res.label === item) {
              this.tableHeaders.push(res)
            }
          })
        })
        this.tableHeader = this.tableHeaders
      } else if (ev.fname === '缺省方案') {
        this.tableHeader = this.oldTabHeader
      } else {
        this.tableHeader = this.tableHeader1
      }
    }
  }
}
</script>
<style lang="scss" scoped>
.content {
  @include listBom;
  .headBtn {
    display: flex;
    .btn1 {
      margin-left: 10px;
    }
  }
  .header{
    position:relative;
    .header-name{
      width: 100%;
    }
    .btn{
      transform: translateY(18%);
      margin-left: 450px !important;
    }
  }
}
</style>
